package com.tyros.web.banner;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.env.Environment;

import javax.annotation.Resource;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Optional;

/**
 * @author zhangshuaiyin
 * @date 2022/7/9 14:20
 */
@Slf4j
public class TyrosRunner implements ApplicationRunner {
    @Resource
    private Environment environment;

    @Resource
    private TyrosProperties tyrosProperties;

    @Override
    public void run(ApplicationArguments args) throws UnknownHostException {
        if (!tyrosProperties.isEnable()) {
            return;
        }
        String content = "\n";
        content += "\n------------------------------------------------------------------------------------------------------------------------------";
        content += "\n ██████████                                        ██                      ██ ██                     ██   ██                  ";
        content += "\n░░░░░██░░░   ██   ██                              ████    ██████  ██████  ░██░░                     ░██  ░░                   ";
        content += "\n    ░██     ░░██ ██  ██████  ██████   ██████     ██░░██  ░██░░░██░██░░░██ ░██ ██  █████   ██████   ██████ ██  ██████  ███████ ";
        content += "\n    ░██      ░░███  ░░██░░█ ██░░░░██ ██░░░░     ██  ░░██ ░██  ░██░██  ░██ ░██░██ ██░░░██ ░░░░░░██ ░░░██░ ░██ ██░░░░██░░██░░░██";
        content += "\n    ░██       ░██    ░██ ░ ░██   ░██░░█████    ██████████░██████ ░██████  ░██░██░██  ░░   ███████   ░██  ░██░██   ░██ ░██  ░██";
        content += "\n    ░██       ██     ░██   ░██   ░██ ░░░░░██  ░██░░░░░░██░██░░░  ░██░░░   ░██░██░██   ██ ██░░░░██   ░██  ░██░██   ░██ ░██  ░██";
        content += "\n    ░██      ██     ░███   ░░██████  ██████   ░██     ░██░██     ░██      ███░██░░█████ ░░████████  ░░██ ░██░░██████  ███  ░██";
        content += "\n    ░░      ░░      ░░░     ░░░░░░  ░░░░░░    ░░      ░░ ░░      ░░      ░░░ ░░  ░░░░░   ░░░░░░░░    ░░  ░░  ░░░░░░  ░░░   ░░ ";
        content += "\nService Name        :  " + environment.getProperty("spring.application.name");
        content += "\nService IP & Port   :  " + "http://" + InetAddress.getLocalHost().getHostAddress() + ":" + environment.getProperty("server.port") + Optional.ofNullable(environment.getProperty("server.servlet.context-path")).orElse("");
        content += "\nService PID         :  " + Long.parseLong(ManagementFactory.getRuntimeMXBean().getName().split("@")[0]);
        content += "\nService JDK Info    :  " + System.getProperty("java.vendor") + " : " + System.getProperty("java.version");
        content += "\nOS Name             :  " + System.getProperty("os.name");
        content += "\n------------------------------------------------------------------------------------------------------------------------------";
        content += "\n";

        log.info(content);
    }
}
